package com.example.controller;

import com.example.pojo.Category;
import com.example.pojo.Result;
import com.example.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    /**
     * 新增文章分类
     *
     * @param category 文章分类对象
     */
    @PostMapping
    public Result<String> add(@RequestBody @Validated(Category.Add.class) Category category) {
        categoryService.add(category);
        return Result.success();
    }

    /**
     * 获取文章分类列表
     *
     * @return 文章数据
     */
    @GetMapping
    public Result<List<Category>> list() {
        List<Category> cs = categoryService.list();
        return Result.success(cs);
    }

    /**
     * 获取文章分类详情
     *
     * @return 分类详情
     */
    @GetMapping("/detail")
    public Result<Category> detail(final Integer id) {
        Category c = categoryService.findById(id);
        return Result.success(c);
    }

    @PutMapping
    public Result<String> update(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 删除文章分类
     *
     * @param id 文章分类id
     */
    @DeleteMapping
    public Result<String> delete(final Integer id) {
        categoryService.delete(id);
        return Result.success();
    }

}
